<?php

namespace app\controller\api\article;

use think\App;
use app\controller\api\Base;
use app\common\services\CacheService;
use app\common\repositories\article\news\NewsRepository;
use app\common\repositories\article\news\NewsCateRepository;

class ArticleNews extends Base
{
    protected $repository;

    public function __construct(App $app, NewsRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    /**
     * 分类列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-27 16:14
     */
    public function getCate(NewsCateRepository $cateRepository)
    {
        $where['is_show'] = 1;
        $res = CacheService::create($this->request->propertyId)
            ->remember('article_news_cate_' . $this->request->propertyId, function () use ($where, $cateRepository) {
                return $cateRepository->getCascaderData($this->request->propertyId, $where);
            }, 3600);
        return app('api_return')->success($res);
    }


    /**
     * 新闻列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-27 16:26
     */
    public function getNewsList()
    {
        $where = $this->request->param([
            'cate_id' => '',
            'keywords' => ''
        ]);
        $where['is_show'] = 1;
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     * 新闻详情
     * @param $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-27 16:34
     */
    public function getDetail($id)
    {
        if (!$this->repository->exists($id)) {
            return app('api_return')->error('数据不存在');
        }
        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $this->repository->incField($info['id'], 'click');
        return app('api_return')->success($info);
    }
}